package models.permission;

import com.google.gson.annotations.Expose;
import models.common.AuditInfo;
import models.common.CodeStatus;
import models.common.UserType;
import models.deadbolt.Role;
import models.deadbolt.RoleHolder;
import play.data.validation.Email;
import play.data.validation.Required;
import play.data.validation.Unique;
import play.libs.Crypto;
import utils.ShouldSkip;
import utils.audit.AuditListener;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunqw on 3/4 0004.
 * 用户登录信息，包含本地登录和微信登陆
 */
@Entity
@Table(name = "tt_user")
@EntityListeners({AuditListener.class})
public class User extends AuditInfo implements RoleHolder{


    /**
     * 用户类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name="user_type")
    public UserType userType;
    /**
     * 用户名
     */
    @Expose
    @Column(name="user_name")
    @Required
    @Unique
    public String userName;

    /**
     * 密码
     */
    @Transient
    public String password;

    /**
     * 密码密文
     */
    @Column(name="pwd")
    @ShouldSkip
    public String encryptedPassword;

    /**
     * 微信号码
     */
    @Column(name="wechatid")
    @ShouldSkip
    public String weChatId;

    /**
     * 微信token
     */
    @Column(name="token")
    @ShouldSkip
    public String token;

    /**
     * 姓名
     */
    @Expose
    @Column(name="name")
    public String name;

    /**
     * 头像
     */
    public String avatar;

    /**
     * 手机号码
     */
    @Column(name = "mobile_number")
    @Required
    @Unique
    public String mobileNumber;

    /**
     * 邮箱
     */
    @Column(name="email")
    @Required
    @Unique
    @Email
    public String email;

    /**
     * 身份证号码
     */
    @Column(name="idNumber")
    public String idNumber;

    /**
     * 启用状态
     */
    @Enumerated(EnumType.STRING)
    public CodeStatus validStatus;

    public void setPassword(String password) {
        //TODO: 检查密码复杂度是否符合要求
        if(password != null) {
            this.password = password;
            this.encryptedPassword = Crypto.encryptAES(password);
        }
    }

    public static boolean authenticate(String username, String password) {
        User user = User.findUserByUserName(username);
        return user != null && user.encryptedPassword.equals(Crypto.encryptAES(password));
    }

    /**
     * 查找用户
     * @param userName, 可以是用户名、手机号或者邮箱
     * @return
     */
    public static User findUserByUserName(String userName){
        User user = User.find("byUserName", userName).first();
        if(user==null){
            user = User.find("byMobileNumber",userName).first();
        }
        if(user==null){
            user = User.find("byEmail",userName).first();
        }
        return user;
    }
    /**
     * 用户对应的角色，一般只针对管理员适用；
     * 设计师和客户的功能点可以不通过角色配置。
     */
    @ManyToOne
    @JoinColumn(name="role_code",referencedColumnName = "code")
    public models.permission.Role role;

    @Override
    public List<? extends Role> getRoles() {
        ArrayList<models.permission.Role> roles = new ArrayList<models.permission.Role>(1);
        roles.add(role);
        return roles;
    }
}
